﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IDAL;
using Model;
using System.Data;
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;

namespace DAL
{
    public class FollowsDAL : IFollowsDAL
    {
        ///FL_followId  FL_IdFrom  FL_IdTo FL_followTime
        /// <summary>
        /// 根据传入的followId判断一条数据是否存在
        /// </summary>       
        public bool followIdExists(int followId)
        {
            //Set up a return value
            bool isExist = false;
            string sqlStatement = "SELECT FL_followId FROM Follows WHERE FL_followId = @followId";

            try
            {
                SqlConnection conn = new SqlConnection("Data Source=localhost;Initial Catalog=Tiamat;Integrated Security=True");

                SqlParameter para;
                para = new SqlParameter("@followId", followId);

                //Execute the query	
                using (SqlDataReader rdr = SqlHelper.ExecuteReader(conn, CommandType.Text, sqlStatement, para))
                {
                    if (rdr.Read())
                        isExist = true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return isExist;

        }


        /// <summary>
        /// 根据传入的IdFrom判断一条数据是否存在
        /// </summary>       
        public bool IdFromExists(int IdFrom)
        {
            //Set up a return value
            bool isExist = false;
            string sqlStatement = "SELECT FL_IdFrom FROM Follows WHERE FL_IdFrom = @IdFrom";

            try
            {
                SqlConnection conn = new SqlConnection("Data Source=localhost;Initial Catalog=Tiamat;Integrated Security=True");

                SqlParameter para;
                para = new SqlParameter("@IdFrom", IdFrom);

                //Execute the query	
                using (SqlDataReader rdr = SqlHelper.ExecuteReader(conn, CommandType.Text, sqlStatement, para))
                {
                    if (rdr.Read())
                        isExist = true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return isExist;

        }



        /// <summary>
        /// 根据传入的IdTo判断一条数据是否存在
        /// </summary>       
        public bool IdToExists(int IdTo)
        {
            //Set up a return value
            bool isExist = false;
            string sqlStatement = "SELECT FL_IdTo FROM Follows WHERE FL_IdTo= @IdTo";

            try
            {
                SqlConnection conn = new SqlConnection("Data Source=localhost;Initial Catalog=Tiamat;Integrated Security=True");

                SqlParameter para;
                para = new SqlParameter("@IdTo", IdTo);

                //Execute the query	
                using (SqlDataReader rdr = SqlHelper.ExecuteReader(conn, CommandType.Text, sqlStatement, para))
                {
                    if (rdr.Read())
                        isExist = true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return isExist;

        }



        /// <summary>
        /// 根据传入的IdFrom和IdTo判断一条数据是否存在
        /// </summary>       
        public bool idFromToExists(int IdFrom, int IdTo)
        {
            //Set up a return value
            bool isExist = false;
            string sqlStatement = "SELECT FL_IdTo FROM Follows WHERE FL_IdTo= @IdTo AND FL_IdFrom=@IdFrom";

            try
            {
                SqlConnection conn = new SqlConnection("Data Source=localhost;Initial Catalog=Tiamat;Integrated Security=True");

                SqlParameter[] paras;
                paras = new SqlParameter[] { new SqlParameter("@IdTo", IdTo), new SqlParameter("@IdFrom", IdFrom) };

                //Execute the query	
                using (SqlDataReader rdr = SqlHelper.ExecuteReader(conn, CommandType.Text, sqlStatement, paras))
                {
                    if (rdr.Read())
                        isExist = true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return isExist;

        }


        /// <summary>
        /// 增加一条数据
        /// </summary>                
        public bool Add(int followId, int IdFrom, int IdTo, System.DateTime followTime)
        {
            //Set up a return value
            bool opetation = false;
            string sqlStatement = "INSERT INTO Follows VALUS(@followId ,@IdFrom,@IdTo,@followTime)";

            try
            {
                SqlConnection conn = new SqlConnection("Data Source=localhost;Initial Catalog=Tiamat;Integrated Security=True");

                SqlParameter[] paras;
                paras = new SqlParameter[] { new SqlParameter("@followId", followId), new SqlParameter("@IdFrom", IdFrom), new SqlParameter("@IdTo", IdTo), new SqlParameter("@followTime", followTime) };

                //Execute the delete	
                int lineNum = SqlHelper.ExecuteNonQuery(conn, CommandType.Text, sqlStatement, paras);
                {
                    if (lineNum > 0)
                        opetation = true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return opetation;
        }


        /// <summary>
        /// 更新一条数据 FL_followId  FL_IdFrom  FL_IdTo FL_followTime
        /// </summary>             
        public bool Update(int followId, int IdFrom, int IdTo, System.DateTime followTime)
        {
            //Set up a return value
            bool opetation = false;
            string sqlStatement = "UPDATE Follows SET FL_IdFrom=@IdFrom,FL_IdTo=@IdTo,FL_followTime=@followTime WHERE FL_followId = @followId";

            try
            {
                SqlConnection conn = new SqlConnection("Data Source=localhost;Initial Catalog=Tiamat;Integrated Security=True");

                SqlParameter[] paras;
                paras = new SqlParameter[] { new SqlParameter("@IdFrom", IdFrom), new SqlParameter("@IdTo", IdTo), new SqlParameter("@followTime", followTime), new SqlParameter("@followId", followId) };

                //Execute the delete	
                int lineNum = SqlHelper.ExecuteNonQuery(conn, CommandType.Text, sqlStatement, paras);
                {
                    if (lineNum > 0)
                        opetation = true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return opetation;
        }




        /// <summary>
        /// 增加一条数据
        /// </summary>                
        public bool Add(Model.Follows follow)
        {
            //Set up a return value
            bool opetation = false;
            string sqlStatement = "INSERT INTO Follows VALUES(@followId ,@IdFrom,@IdTo,@followTime)";

            try
            {
                SqlConnection conn = new SqlConnection("Data Source=localhost;Initial Catalog=Tiamat;Integrated Security=True");

                SqlParameter[] paras;
                paras = new SqlParameter[] { new SqlParameter("@followId", Convert.ToInt32(follow.getFL_followId())), new SqlParameter("@IdFrom", Convert.ToInt32(follow.getFL_IdFrom())), new SqlParameter("@IdTo", Convert.ToInt32(follow.getFL_IdTo())), new SqlParameter("@followTime", Convert.ToDateTime(follow.getFL_followTime())) };

                //Execute the delete	
                int lineNum = SqlHelper.ExecuteNonQuery(conn, CommandType.Text, sqlStatement, paras);
                {
                    if (lineNum > 0)
                        opetation = true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return opetation;
        }


        /// <summary>
        /// 更新一条数据
        /// </summary>             
        public bool Update(Model.Follows follow)
        {
            //Set up a return value
            bool opetation = false;
            string sqlStatement = "UPDATE Follows SET FL_IdFrom=@IdFrom,FL_IdTo=@IdTo,FL_followTime=@followTime WHERE FL_followId = @followId";

            try
            {
                SqlConnection conn = new SqlConnection("Data Source=localhost;Initial Catalog=Tiamat;Integrated Security=True");

                SqlParameter[] paras;
                paras = new SqlParameter[] { new SqlParameter("@followId", Convert.ToInt32(follow.getFL_followId())), new SqlParameter("@IdFrom", Convert.ToInt32(follow.getFL_IdFrom())), new SqlParameter("@IdTo", Convert.ToInt32(follow.getFL_IdTo())), new SqlParameter("@followTime", Convert.ToDateTime(follow.getFL_followTime())) };

                //Execute the delete
                int lineNum = SqlHelper.ExecuteNonQuery(conn, CommandType.Text, sqlStatement, paras);
                {
                    if (lineNum > 0)
                        opetation = true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return opetation;
        }


        /// <summary>
        /// 根据传入的followId删除一条数据
        /// </summary>
        public bool followIdDelete(int followId)
        {
            //Set up a return value
            bool opetation = false;
            string sqlStatement = "DELETE FROM Follows WHERE FL_followId = @followId";

            try
            {
                SqlConnection conn = new SqlConnection("Data Source=localhost;Initial Catalog=Tiamat;Integrated Security=True");

                SqlParameter para;
                para = new SqlParameter("@followId", followId);

                //Execute the delete	
                int lineNum = SqlHelper.ExecuteNonQuery(conn, CommandType.Text, sqlStatement, para);
                {
                    if (lineNum > 0)
                        opetation = true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return opetation;
        }



        /// <summary>
        /// 根据followId得到一个对象实体
        /// </summary>
        public Model.Follows GetModel(int followId)
        {

            //Set up a return value
            Model.Follows follows = null;

            string sqlStatement = "SELECT FL_followId , FL_IdFrom , FL_IdTo ,FL_followTime FROM Follows WHERE FL_followId = @followId";

            try
            {
                SqlConnection conn = new SqlConnection("Data Source=localhost;Initial Catalog=Tiamat;Integrated Security=True");

                SqlParameter para;
                para = new SqlParameter("@followId", followId);

                //Execute the query	
                using (SqlDataReader rdr = SqlHelper.ExecuteReader(conn, CommandType.Text, sqlStatement, para))
                {
                    if (rdr.Read())
                    {
                        follows.setFL_followId(Convert.ToInt32(rdr.GetString(0)));
                        follows.setFL_IdFrom(Convert.ToInt32(rdr.GetString(1)));
                        follows.setFL_IdTo(Convert.ToInt32(rdr.GetString(2)));
                        follows.setFL_followTime(Convert.ToDateTime(rdr.GetString(3)));
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return follows;

        }



    }
}
